//
//  Constants.swift
//  beegin
//
//  Created by zsy on 15/10/24.
//  Copyright © 2015年 kszz. All rights reserved.
//

import Foundation

let arryComponents:[[String]] = [["1","5","10","20","50","75","100"],["克","毫克","微克","千微克","毫升","微升","微升"]]

enum FoodType : Int{

    case Water = 1
    case Breast = 2
    case Milk = 3
    case Supplement = 4
     case liquid = 5
    case Material = 6
    case CustomFood = 7
}

enum BrandType : Int{

    case BrandFood = 2
    case BrandMaterial = 1
   
}

struct BabyEntity {
    
    var rawDictionary:[String:AnyObject]{
    
        return ["birthday":birthday,"height":height,"sex":sex,"weight":weight,"customer.id":customer_id]
    }
    
    init(_ dic:[String:AnyObject]?){
        
        id              = (dic?["id"] as? Int) ?? -1
        birthday        = (dic?["birthday"] as? String) ?? "2010-01-01"
        height          = (dic?["height"] as? Int) ?? 0
        sex             = (dic?["sex"] as? Int) ?? 0
        weight          = (dic?["weight"] as? Int) ?? 0
        customer_id     = (dic?["customer.id"] as? Int) ?? -1
        
        let array = birthday.componentsSeparatedByString(" ")
        if array.count > 1{
        
            birthday = array[0]
        }
    }
    
    var id:Int
    var birthday:String
    var height:Int
    var sex:Int
    var weight:Int
    var customer_id:Int
}

struct CustomerEntity {
    
    var rawDictionary:[String:AnyObject]
    
    init(_ dic:[String:AnyObject]){
        rawDictionary       = dic
        id                  = dic["id"] as! Int
        avatar              = dic["avatar"] as! String
        createDatetime      = dic["createDatetime"] as! String
        email               = dic["email"] as! String
        level               = dic["level"] as! Int
        loginid             = dic["loginid"] as! String
        nickname            = dic["nickname"] as! String
        password            = dic["password"] as! String
        refreshTime         = dic["refreshTime"] as! String
        token               = dic["token"] as! String

    }
    
    var id:Int
    var avatar:String
    var createDatetime:String
    var email:String
    var level:Int
    var loginid:String
    var nickname:String
    var password:String
    var refreshTime:String
    var token:String
    
    var babeids:[Int] = []
    var favoriteids:[Int] = []
}

struct BrandEntity {
    
    var rawDictionary:[String:AnyObject]
    
    init(_ dic:[String:AnyObject]){
        rawDictionary   = dic
        id          = dic["id"] as! Int
        name        = dic["name"] as! String
        picurl      = dic["picurl"] as! String
        typeid      = dic["typeid"] as! Int
        nameEn      = (dic["nameEn"] as? String) ?? ""
    }
    
    var id:Int
    var name:String
    var picurl:String
    var typeid:Int
    var nameEn:String
}

struct FavoriteEntity {
    var rawDictionary:[String:AnyObject]
    init(_ dic:[String:AnyObject]){
        rawDictionary       = dic
        id                  = (dic["id"] as! Int) ??  -1
        food_id             = (dic["foodid"] as! Int) ?? -1
    }
    
    var id:Int
    var food_id:Int
}

struct RecordEntity {
    var rawDictionary:[String:AnyObject]{
        
        var dic =  ["count":count,"flagid":flagid,"unit":unit,"state":state,"syncid":syncid,"food.id":record_food.id,"baby.id":baby_id,"customer.id" : customer_id,"recordTime" : NSDate(timeInterval: 60 * 60 * 8, sinceDate: record_time)]
        
        if id != -1{
        
            dic["id"] = id
        }
        
        return dic
    }
    init(food:FoodEntity,_ babyid:Int? = -1,_ dic:[String:AnyObject]? = nil){
        
        
        id                  = (dic?["id"] as? Int) ?? -1
        count               = (dic?["count"] as? Int) ?? 1
        flagid              = (dic?["flagid"] as? Int) ?? 1
        state               = (dic?["state"] as? Int) ?? 0
        syncid              = (dic?["syncid"] as? String) ?? OCUtils.gen_uuid()
        unit                = (dic?["unit"] as? Int) ?? 0
        baby_id             = (dic?["baby.id"] as? Int) ?? 0
        food_id             = (dic?["food_id"] as? Int) ?? 0
        customer_id         = (dic?["customer.id"] as? Int) ?? 0
        record_food         = food
        
        if let dateString = dic?["recordTime"] as? String{
            
            let format = NSDateFormatter()
            format.dateFormat = "yyyy-MM-dd HH:mm:ss"
            record_time =  format.dateFromString(dateString) ?? NSDate()
        }else{
            //排序放在最后
            
            record_time         =  NSDate(timeIntervalSinceNow: 60 * 60 * 24 * 365 * 10)
        }
        
        baby_id = babyid ?? 0
    }
    
    var id:Int
    var count:Int
    var flagid:Int
    var record_time:NSDate
    var state:Int
    var syncid:String
    var unit:Int
    var baby_id:Int
    var food_id:Int
    var customer_id:Int
    
    var record_food:FoodEntity
}

class FoodEntity : NSObject {
    var rawDictionary:[String:AnyObject]
    init(_ dic:[String:AnyObject]){
        
        let type = dic["typeid"] as! Int
        
        rawDictionary = dic
        id            = (dic["id"] as? Int) ?? 1
        approval      = (dic["approval"] as? Int) ?? 1
        foodid        = (dic["foodid"] as? Int) ?? 1
        name          = (dic["name"] as? String) ?? ""
        typeid        = type
        picurl        = (dic["picurl"] as? String) ?? ""
        brandid       = (dic["brandid"] as? Int) ?? 0
        orgin_id      = (dic["orgin_id"] as? Int) ?? 0
        
        switch FoodType(rawValue: typeid)!{
        case .Breast:
            picurl = "default_breast"
        case .Water:
            picurl = "defualt_water"
        case .Milk:
            picurl = picurl.isEmpty ? "defualt_milk" : picurl
        case .Supplement:
            picurl = picurl.isEmpty ? "default_food_1" : picurl
        case.CustomFood:
            picurl = picurl.isEmpty ? "default_food_1" : picurl
        case .Material:
            picurl = picurl.isEmpty ? "default_food_1" : picurl
        case .liquid:
            picurl = picurl.isEmpty ? "default_food_1" : picurl
        }
    }
    
    var id:Int
    var approval:Int
    var foodid:Int
    var name:String
    var picurl:String
    var typeid:Int
    var brandid:Int
    var orgin_id:Int
}


struct MaterialEntity {
    var rawDictionary:[String:AnyObject]
    init(_ dic:[String:AnyObject]){
        rawDictionary   = dic
        id              = dic["id"] as! Int
        name            = dic["name"] as! String
        picurl          = dic["picurl"] as! String
        brand_id        = dic["brand_id"] as! Int
        element_id      = dic["element_id"] as! Int
    }
    
    var id:Int
    var name:String
    var picurl:String
    var brand_id:Int
    var element_id:Int
}

struct FormulalEntity {
    var rawDictionary:[String:AnyObject]{
        
        return ["food.id":materialid,"uint":uint,"count":count,"sort" : sort]
        
    }
    
    init(dic:[String:AnyObject]){
        id                  = (dic["id"] as? Int) ?? -1
        materialid          = (dic["foodid"] as? Int) ?? -1
        count               = (dic["count"] as? Int) ?? 0
        uint                = (dic["uint"] as? Int) ?? 0
        sort                = (dic["sort"] as? Int) ?? 1
    }
    
    var id:Int
    var materialid:Int
    var uint:Int
    var count:Int
    var sort:Int
    
    var countString:String{
    
        return "\(count)"
    }
    
    var unitString:String{
    
        return arryComponents[1][uint]
    }
}

struct CustomFoodEntity {
    var rawDictionary:[String:AnyObject]{
        
        var dic:[String:AnyObject] = ["name":name,"picurl":picurl,"customer.id":customer_id]
        for k in 0...formalals.count - 1{
        
            dic["formulaDatas[\(k)].food.id"] = formalals[k].materialid
            dic["formulaDatas[\(k)].count"] = formalals[k].count
            dic["formulaDatas[\(k)].sort"] = formalals[k].sort
            dic["formulaDatas[\(k)].unit"] = formalals[k].uint
        }
        
        return dic
    }
    init(dic:[String:AnyObject]){
        
        id                  = (dic["id"] as? Int) ?? -1
        name               = (dic["name"] as? String) ?? ""
        picurl              = (dic["picurl"] as? String) ?? ""
        customer_id         = (dic["customer.id"] as? Int) ?? 0
        
        if let formalalDatas = dic["formulaDatas"] as? [[String : AnyObject]]{
        
            for item in formalalDatas{
            
                formalals.append(FormulalEntity(dic: item))
            }
        }
    }
    
    init(initFood:FoodEntity,initFormulals:[FormulalEntity],customerid:Int){
    
        id              = initFood.foodid
        name            = initFood.name
        picurl          = initFood.picurl
        customer_id     =  customerid
        formalals       = initFormulals
    }
    
    var id:Int
    var name:String
    var picurl:String
    var customer_id:Int
    
    var formalals:[FormulalEntity] = []
}

struct MilkEntity {
    var rawDictionary:[String:AnyObject]
    init(_ dic:[String:AnyObject]){
        rawDictionary       = dic
        id                  = dic["id"] as! Int
        crate_datetime      = dic["crate_datetime"] as! Int
        name                = dic["name"] as! String
        name_en             = dic["name_en"] as! String
        page                = dic["page"] as! String
        phase               = dic["phase"] as! String
        picurl              = dic["picurl"] as! String
        target_group        = dic["target_group"] as! String
        version             = dic["version"] as! String
        brand_id            = dic["brand_id"] as! Int
        element_id          = dic["element_id"] as! Int
        orgin_id            = dic["orgin_id"] as! Int
    }
    
    var id:Int
    var crate_datetime:Int
    var name:String
    var name_en:String
    var page:String
    var phase:String
    var picurl:String
    var target_group:String
    var version:String
    var brand_id:Int
    var element_id:Int
    var orgin_id:Int
}

class OrginEntity : NSObject{
    var rawDictionary:[String:AnyObject]
    init(_ dic:[String:AnyObject]){
        rawDictionary   = dic
        id              = dic["id"] as! Int
        name            = dic["name"] as! String
        nameEn         = dic["nameEn"] as! String
        picurl          = dic["picurl"] as! String
        foodids         = dic["foodids"] as! String
        sortid          = dic["sortid"] as! Int
    }
    
    var id:Int
    var name:String
    var nameEn:String
    var picurl:String
    var foodids:String
    var sortid:Int
}

struct SupplementEntity {
    var rawDictionary:[String:AnyObject]
    init(_ dic:[String:AnyObject]){
        rawDictionary       = dic
        id                  = dic["id"] as! Int
        crate_datetime      = dic["crate_datetime"] as! Int
        name                = dic["name"] as! String
        name_en             = dic["name_en"] as! String
        page                = dic["page"] as! String
        phase               = dic["phase"] as! String
        picurl              = dic["picurl"] as! String
        target_group        = dic["target_group"] as! String
        version             = dic["version"] as! String
        brand_id            = dic["brand_id"] as! Int
        element_id          = dic["element_id"] as! Int
        orgin_id            = dic["orgin_id"] as! Int
    }
    
    var id:Int
    var crate_datetime:Int
    var name:String
    var name_en:String
    var page:String
    var phase:String
    var picurl:String
    var target_group:String
    var version:String
    var brand_id:Int
    var element_id:Int
    var orgin_id:Int
}

enum DataResponseType : Int{

    case ResponseItem,ResponseList
}

struct Constants{
    
    static let url:String = "http://api.beegin.cn/server"
    static let registerPath = "/customer"
    static let loginPath = "/customer/login"
    static let babyPath = "/customer/baby"
    static let recordPath = "/customer/record"
    static let foodPath = "/food/customer/"
    static let brandPath = "/food/brand"
    static let orginPath = "/food/orgin"
    static let foodElementsPath = "/food/"
    static let milkPath = "/food/milk"
    static let supplementPath = "/food/supplement"
    static let customPath = "/food/custome/"
    static let materialPath = "/food/material"
    static let favoritePath = "/customer/favorite"
    static let filePath = "http://api.beegin.cn/server/resource/file"
    static let fileUrl = "http://api.beegin.cn/server/upload/"
    
    static let loginInfo = "loginInfo"
    
    
    static let kDataReflashNotification = "LOCAL_DATA_REFLASH_NOTIFICATION"
}

var defaultMilkBrands:[BrandEntity]{

    let json = JSON(data: NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("MilkBrand", ofType: "json")!)!)
    var brands:[BrandEntity] = []
    if let objects = json["data"].array{
    
        for item in objects{
        
            if let data = item.dictionaryObject{
            
                brands.append(BrandEntity(data))
            }
        }
    }
    return brands
}


var defaultSuplementBrands:[BrandEntity]{
    
    let json = JSON(data: NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("SuplementBrand", ofType: "json")!)!)
    var brands:[BrandEntity] = []
    if let objects = json["data"].array{
        
        for item in objects{
            
            if let data = item.dictionaryObject{
                
                brands.append(BrandEntity(data))
            }
        }
    }
    return brands
}

var defaultOrgins:[OrginEntity]{
    
    let json = JSON(data: NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("Orgin", ofType: "json")!)!)
    var orgins:[OrginEntity] = []
    if let objects = json["data"].array{
        
        for item in objects{
            
            if let data = item.dictionaryObject{
                
                orgins.append(OrginEntity(data))
            }
        }
    }
    
    return orgins
}

var defaultFoods:[FoodEntity]{
    
    let json = JSON(data: NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("Food", ofType: "json")!)!)
    var foods:[FoodEntity] = []
    if let objects = json["data"].array{
        
        for item in objects{
            
            if let data = item.dictionaryObject{
                
                foods.append(FoodEntity(data))
            }
        }
    }
    
    return foods
}

